package com.redis.seckill.mq.service;

import com.redis.seckill.mq.config.ActiveMQConfig;
import lombok.extern.log4j.Log4j2;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 发送者
 * @date 2020/10/29 9:08
 */
@Service
@Log4j2
public class Sender {


    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    /**
     * 如果用户抢到商品就发送到队列中去
     * 信息包含用户id和商品id
     * @param userId
     * @param productId
     */
    public void sendDirectQueue(Integer userId,Integer productId,Integer quantity){
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>秒杀请求发送，商品id:"+productId+"用户id"+userId);
        try {
            Map<String,Integer> map = new HashMap<>();
            map.put("userId",userId);
            map.put("productId",productId);
            map.put("quantity",quantity);
            jmsMessagingTemplate.convertAndSend(ActiveMQConfig.QUEUE_NAME,map);
        }catch (Exception e){
            e.printStackTrace();
            log.error("【队列发送异常】{}",e.getMessage());
        }
    }
}
